﻿using System;
using System.Windows;
using System.Windows.Controls;
using KPM.Common;

namespace KPM.Common.DragHelpers
{
    public class CanvasDragDefinition : DragDefinition
    {
        private double _startLeft;
        private double _startTop;

        public CanvasDragDefinition(Func<bool> canDrag, double maxLeft, double maxTop)
            : base(canDrag)
        {
            MaxLeft = maxLeft;
            MaxTop = maxTop;
        }

        public double MaxLeft { get; set; }
        public double MaxTop { get; set; }

        protected override void HandleDragStarted(UIElement element)
        {
            _startLeft = Canvas.GetLeft(element);
            _startTop = Canvas.GetTop(element);
        }

        protected override void HandleDragMove(UIElement element, Point currentPoint, Point overallMovement)
        {
            var left = _startLeft + overallMovement.X;
            var top = _startTop + overallMovement.Y;
            if (MaxLeft > 0)
            {
                //left = left.Limit(0, MaxLeft);
                left = MaxLeft;
            }

            if (MaxTop > 0)
            {
                //top = top.Limit(0, MaxTop);
                top = MaxTop;
            }

            switch (DragType)
            {
                case CanvasDragType.LeftOnly:
                    Canvas.SetLeft(element, left);
                    break;
                case CanvasDragType.TopOnly:
                    Canvas.SetTop(element, top);
                    break;
                case CanvasDragType.LeftAndTop:
                    Canvas.SetLeft(element, left);
                    Canvas.SetTop(element, top);
                    break;
                default:
                    break;
            }
        }
    }
}